package org.redhat.auth.security.user;

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.util.AntUrlPathMatcher;

/**
 * 扩展User类，加入一些其它属性
 * 
 * @author lingen.liu
 * 
 */
public class UserDetailInfo extends User {
	private static final long serialVersionUID = 9188494832615361052L;

	/* 用户真实姓名 */
	protected String realName;

	/* 用户Email */
	protected String email;

	/* 用户注册日期 */
	protected Date registryDate;

	/* 上次修改密码日期 */
	protected Date passwordLastUpdateDate;

	/* 用户描述 */
	protected String description;

	/*
	 * 检测是否是测试用户
	 */
	protected boolean isTestUser;

	protected Map<String, Collection<ConfigAttribute>> roleEntityMap = new HashMap<String, Collection<ConfigAttribute>>();

	public UserDetailInfo(String username, String password, boolean enabled,
			boolean accountNonExpired, boolean credentialsNonExpired,
			boolean accountNonLocked, Collection<GrantedAuthority> authorities) {
		super(username, password, enabled, accountNonExpired,
				credentialsNonExpired, accountNonLocked, authorities);
	}

	public boolean hasRole(String roleName) {
		if (roleName == null) {
			return false;
		}
		if (!roleName.startsWith("ROLE_")) {
			roleName = "ROLE_" + roleName;
		}
		return getAuthorities().contains(new GrantedAuthorityImpl(roleName));
	}

	public boolean hasAuthorize(String url) {
		if (url == null) {
			return false;
		}
		AntUrlPathMatcher urlMatcher = new AntUrlPathMatcher();
		for (Entry<String, Collection<ConfigAttribute>> entry : getRoleEntityMap()
				.entrySet()) {
			if (urlMatcher.pathMatchesUrl(entry.getKey(), url)) {
				return true;
			}
		}
		return false;
	}
	
	public static void main(String[] args) {
		AntUrlPathMatcher urlMatcher = new AntUrlPathMatcher();
		System.out.println(urlMatcher.pathMatchesUrl("/*", "/admin/Aadmin-test.jsp"));
	}

	//
	public String getRealName() {
		return realName;
	}

	public String getEmail() {
		return email;
	}

	public Date getRegistryDate() {
		return registryDate;
	}

	public Date getPasswordLastUpdateDate() {
		return passwordLastUpdateDate;
	}

	public String getDescription() {
		return description;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public void setRegistryDate(Date registryDate) {
		this.registryDate = registryDate;
	}

	public void setPasswordLastUpdateDate(Date passwordLastUpdateDate) {
		this.passwordLastUpdateDate = passwordLastUpdateDate;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public boolean isTestUser() {
		return isTestUser;
	}

	public void setTestUser(boolean isTestUser) {
		this.isTestUser = isTestUser;
	}

	public Map<String, Collection<ConfigAttribute>> getRoleEntityMap() {
		return roleEntityMap;
	}

	public void setRoleEntityMap(
			Map<String, Collection<ConfigAttribute>> roleEntityMap) {
		this.roleEntityMap = roleEntityMap;
	}
}
